import request from '@/utils/request'

// 查询班组列表
export function listTeam(query) {
 // 由于后端使用startPage()方法获取分页参数，必须以固定方式传递
  // 若依后端的startPage()使用pageNum和pageSize作为参数名称
  const pageNum = parseInt(query.pageNum) || 1;
  const pageSize = parseInt(query.pageSize) || 10;
  
  console.log("API方法收到分页参数: pageNum=" + pageNum + ", pageSize=" + pageSize);
  
  // 构建API请求参数
  const params = { ...query };
  
  // 处理模糊搜索参数
  if (query.team_name) {
    console.log("处理模糊搜索参数:", {
      team_name: query.team_name,
    });
  }
  
 
  
  // 处理班组状态参数
  if (query.team_status !== undefined) {
    console.log("使用班组状态过滤:", query.team_status);
    // 确保状态参数是整数
    params.team_status = parseInt(query.team_status);
  }
  
  // 移除可能导致问题的参数
  delete params.pageNum;
  delete params.pageSize;
  
  console.log("API方法构建的请求参数:", JSON.stringify(params));
  
  // 使用若依框架的正确参数名称
  // 使用URL参数方式传递分页参数，避免与请求体冲突
  return request({
    url: '/ldzl-basic/team/list',
    method: 'post',
    // 必须添加params参数，而不是放在data中，因为startPage()会从params中获取分页信息
    params: {
      pageNum: pageNum,
      pageSize: pageSize
    },
    // 业务数据保持在body中
    data: params
  })
}

// 查询班组详细
export function getTeam(teamId) {
  console.log("调用getTeam API，查询班组详情，teamId:", teamId);
  return request({
    url: '/ldzl-basic/team/detail/' + teamId,
    method: 'post'
  }).then(response => {
    console.log("getTeam API响应:", response);
    return response;
  }).catch(error => {
    console.error("getTeam API错误:", error);
    throw error;
  });
}

// 新增班组
export function addTeam(data) {
  return request({
    url: '/ldzl-basic/team',
    method: 'post',
    data: data
  })
}

// 修改班组
export function updateTeam(data) {
  console.log("调用updateTeam API，修改班组，data:", data);
  return request({
    url: '/ldzl-basic/team',
    method: 'put',
    data: data
  }).then(response => {
    console.log("updateTeam API响应:", response);
    return response;
  }).catch(error => {
    console.error("updateTeam API错误:", error);
    throw error;
  });
}

// 删除班组
export function delTeam(teamId) {
  console.log("调用delTeam API，删除班组，teamId:", teamId);
  return request({
    url: '/ldzl-basic/team/dp/' + teamId,
    method: 'put'
  }).then(response => {
    console.log("delTeam API响应:", response);
    return response;
  }).catch(error => {
    console.error("delTeam API错误:", error);
    throw error;
  });
}

// 导出班组
export function exportTeam(query) {
  return request({
    url: '/basic/team/export',
    method: 'get',
    params: query
  })
} 


// 查询车间详细
export function getworkshop(data) {
  console.log("调用getworkshop API，参数:", data);
  return request({
    url: '/ldzl-basic/wsp/find',
    method: 'post',
    data: data
  }).then(response => {
    console.log("getworkshop API响应:", response);
    return response;
  }).catch(error => {
    console.error("getworkshop API错误:", error);
    throw error;
  });
}

// 查询生产线详细
export function getlines(data) {
  console.log("调用getlines API，参数:", data);

  // 检查参数中是否包含basicWorkshop
  if (data.basicWorkshop && data.basicWorkshop.workshop_id) {
    console.log("根据车间ID查询生产线，workshop_id:", data.basicWorkshop.workshop_id);
  }

  // 检查参数中是否包含basicProductionline
  if (data.basicProductionline && data.basicProductionline.production_line_id) {
    console.log("查询生产线详情，production_line_id:", data.basicProductionline.production_line_id);
  }

  return request({
    url: '/ldzl-basic/prt/find',
    method: 'post',
    data: data
  }).then(response => {
    console.log("getlines API响应:", response);
    return response;
  }).catch(error => {
    console.error("getlines API错误:", error);
    throw error;
  });
}

//查询员工列表
export function listuser(query) {
  return request({
    url: '/system/user/list',
    method: 'get',
    params: query // 确保参数作为URL查询参数发送，而不是请求体
  })
} 

// 批量删除客户
export function delCustomers(ids) {
  return request({
    url: '/ldzl-basic/team/batch/' + ids,
    method: 'put'
  })
}